package com.xd.pre.modules.tpshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xd.pre.modules.sys.mapper.SysUserMapper;
import com.xd.pre.modules.tpshop.domain.TpGoodsCategory;
import com.xd.pre.modules.tpshop.mapper.TpGoodsCategoryMapper;
import com.xd.pre.modules.tpshop.service.ITpGoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@Service
public class TpGoodsCategoryServiceImpl extends ServiceImpl<TpGoodsCategoryMapper, TpGoodsCategory> implements ITpGoodsCategoryService {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    TpGoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    SysUserMapper userMapper;

    //测试方法
    @Override
    public TpGoodsCategory getOne2() {
        return goodsCategoryMapper.getOne2();
    }

    @Override
    public List<TpGoodsCategory> findgoodsCategoryList(int parseInt) {
        LambdaQueryWrapper<TpGoodsCategory> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(TpGoodsCategory::getParentId, parseInt);
        return list(lambdaQuery);
    }

    //获取根据一级分类获取对应的二三级分类
    //* parentIdPath： 家族图谱 id(本项目中定义最大3级标题！)   类似“0_1”格式的是一级标题, 类似“0_1_12”格式的是二级标题,类似“0_1_12_12”格式的是二级标题, 第二位永远表示一级标题的某个item！
    //* 所以此处的parentID等于几就代表 当前你点击的那个一级标题的item(即：keyId)
    @Override
    public List<TpGoodsCategory> findgoodsSecAndThirdCategoryList(int key_id) {
        String str1 = "%" +"0"+ "\\_" + key_id + "\\_" + "%";
        String str2 = "0"+ "\\_" + key_id + "\\_" ;

        /*
	    String hql1="from TpGoodsCategory WHERE parentIdPath LIKE '%"+"0\\_"+key_id+"\\_"+"%' AND isShow=1 "; 也可以
	    */

        //SELECT *FROM `tp_goods_category` gc WHERE gc.`parent_id_path` LIKE '%0\_1\_%' ## 此处必须这么写！不能写成'%0_1_%' ！！
//        String hql="from TpGoodsCategory WHERE parentIdPath LIKE '%"+ str2 +"%' AND isShow=1 ORDER BY categoryId ASC";  	// '%0\\_?\\_%'这样的写法变异不通过！
        String hql1 = "select * from tp_goods_category WHERE parent_id_path LIKE '%"+ str2 +"%' AND is_show = 1 ORDER BY category_id ASC";  	// '%0\\_?\\_%'这样的写法变异不通过！
        //$list = M('GoodsCategory')->where("parent_id_path LIKE '%0\\_{$parent_id}\\_%' AND is_show=1")->getField('id,mobile_name,image, level , parent_id');

//        String hql2="select * from tp_goods_category WHERE parent_id_path LIKE '%"+ "0"+ "\\_" + "?" + "\\_" +"%' AND is_show = 1 ORDER BY category_id ASC";  	// '%0\\_?\\_%'这样的写法变异不通过！

        List<TpGoodsCategory> result = jdbcTemplate
                .query(hql1, new Object[]{}, new RowMapper<TpGoodsCategory>() {
//                .query(hql2, new Object[]{key_id}, new RowMapper<TpGoodsCategory>() {
                    @Override
                    public TpGoodsCategory mapRow(ResultSet rs, int rowNum) throws SQLException {
                        return result2po(rs);
                    }
                });

        return result;
    }

    private TpGoodsCategory result2po(ResultSet result) throws SQLException {
        TpGoodsCategory moneyPO = new TpGoodsCategory();
        moneyPO.setCategoryId(result.getInt("category_id"));
        moneyPO.setMobileName(result.getString("mobile_name"));
        moneyPO.setParentId(result.getInt("parent_id"));
        moneyPO.setParentIdPath(result.getString("parent_id_path"));
        moneyPO.setLevel(result.getInt("level"));
        moneyPO.setSortOrder(result.getInt("sort_order"));
        moneyPO.setIsShow(result.getInt("is_show"));
        moneyPO.setImage(result.getString("image"));
        moneyPO.setIsHot(result.getInt("is_hot"));
        moneyPO.setCatGroup(result.getInt("cat_group"));
        moneyPO.setCommissionRate(result.getInt("commission_rate"));
        return moneyPO;
    }
}
